<template>
  <Layout>
    <Header>
      <router-link
        class="login-header-logo"
        to="/"
      >
        <SvgIcon
          class="login-header-logo"
          data="@icon/logo.svg"
        />
        <h1 class="login-header-title">
          音域
        </h1>
      </router-link>
    </Header>
    <Content>
      <RadioGroup
        v-model="tab"
        type="button"
        class="page-tabs"
      >
        <Radio label="login">
          登录
        </Radio>
        <Radio label="register">
          注册
        </Radio>
      </RadioGroup>
      <LoginForm
        v-if="tab === 'login'"
        :loading="loading"
        @on-submit="handleLogin"
      />
      <RegisterForm
        v-if="tab === 'register'"
        :loading="loading"
        @on-submit="handleRegister"
      />
    </Content>
    <Footer />
  </Layout>
</template>

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator'
import { UserModule } from '@/store/modules/user'
import LoginForm from './LoginForm.vue'
import RegisterForm from './RegisterForm.vue'
import { register } from '@/api/users'
import { CaptchaChallenge } from '@/api/captcha'

@Component({
  name: 'Login',
  components: {
    LoginForm,
    RegisterForm
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      (vm as {fromPath: string} & Vue).fromPath = from.path
    })
  }
})
export default class extends Vue {
  private loading = false;
  private tab = 'login';

  beforeCreate () {
    if (UserModule.token !== '') {
      this.$Message.info('您已登录')
      this.$router.back()
    }
  }

  // private async handleLogin (form: {username: string; password: string; captchaKey: string; captchaValue: string}) {
  private async handleLogin (form: {username: string; password: string}) {
    this.loading = true
    const last = (this as any).fromPath
    UserModule.Login(form).then(() => {
      this.$Notice.success({
        title: '登录成功',
        desc: last === '/' ? '即将跳转到首页...' : '即将返回上一页面',
        onClose: () => {
          this.$router.push(last)
        }
      })
    }).catch(err => {
      console.log(err)
      this.loading = false
      this.$Notice.error({
        title: '登录失败',
        desc: err
      })
    })
  }

  private async handleRegister (form: any) {
    this.loading = true
    register(form, new CaptchaChallenge(form.captchaKey, form.captchaValue)).then(() => {
      this.$Notice.success({
        title: '注册成功',
        desc: '正在为您返回登录...',
        onClose: () => {
          this.tab = 'login'
        }
      })
    }).catch(err => {
      this.$Notice.error({
        title: '注册失败',
        desc: err
      })
    }).finally(() => {
      this.loading = false
    })
  }
}

</script>

<style lang="less" scoped>
@import "../../styles/theme";

@logo-size: 64px;
@logo-title-size: 35px;

.ivu-layout-header,
.ivu-layout-content,
.ivu-layout-footer {
  display: flex;
  justify-content: center;
}

.ivu-layout-header {
  height: 120px;
  line-height: 120px;
}

.ivu-layout-content {
  /* header 64 | fotter 69 */
  min-height: calc(100vh - 120px - 69px);
  width: 100%;
  padding: 0 50px;
  position: relative;
  overflow: hidden;
  flex-direction: column;
  align-items: center;
  .page-tabs {
    display: block;
    margin: 0 auto 32px auto;
    label {
      background: @background-color-base;
    }
  }
}

.login-header-logo {
  display: flex;
  align-content: center;
  &:hover {
    .login-header-logo {
      color: lighten(@primary-color, 10%);
    }
    .login-header-title {
      color: lighten(@primary-color, 10%);
    }
  }
  .login-header-logo,
  .login-header-title {
    display: inline-block;
  }
  .login-header-logo {
    margin: auto 6px auto 0;
    width: @logo-size;
    height: @logo-size;
    color: @primary-color;
  }
  .login-header-title {
    font-size: @logo-title-size;
    color: @text-color;
    transition: all 0.2s ease-in-out;
  }
}
</style>
